import 'package:go_router/go_router.dart';
import 'package:flutter/material.dart';
import '../pages/main/launch_gateway_screen.dart';
import '../pages/main/main_container_screen.dart';
import '../pages/auth/credential_entry_screen.dart';
import '../pages/auth/account_creation_screen.dart';
import '../pages/home/kite_culture_screen.dart';
import '../pages/home/kite_tutorial_screen.dart';
import '../pages/home/kite_moment_detail_screen.dart';
import '../pages/home/kite_video_player_screen.dart';
import '../pages/community/kite_topic_detail_screen.dart';
import '../pages/community/kite_exchange_detail_screen.dart';
import '../pages/information/kite_team_chat_screen.dart';
import '../pages/information/kite_private_chat_screen.dart';
import '../pages/profile/other_persona_screen.dart';
import '../pages/profile/settings_screen.dart';
import '../pages/information/kite_video_call_screen.dart';
import '../pages/release/release_screen.dart';
import '../state/moment/kite_moment_entity.dart';
import '../state/topic/kite_topic_entity.dart';
import '../state/exchange/kite_exchange_entity.dart';
import '../state/team/kite_team_entity.dart';
import '../state/chat/kite_chat_session_entity.dart';
import '../state/person/persona_entity.dart';

/// 应用导航管理器 - 使用非常规类名避免重复识别
class AppNavigationManager {
  static final AppNavigationManager _singletonInstance =
      AppNavigationManager._privateConstructor();

  static AppNavigationManager get globalInstance => _singletonInstance;

  AppNavigationManager._privateConstructor();

  late final GoRouter _routerInstance;

  /// 过渡动画
  static Widget _buildUniversalPageTransition(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return SlideTransition(
      position: animation.drive(
        Tween(
          begin: const Offset(1.0, 0.0),
          end: Offset.zero,
        ).chain(CurveTween(curve: Curves.easeInOut)),
      ),
      child: FadeTransition(opacity: animation, child: child),
    );
  }

  void initializeRouterConfiguration() {
    _routerInstance = GoRouter(
      initialLocation: '/launch-gateway',
      routes: [
        GoRoute(
          path: '/launch-gateway',
          pageBuilder:
              (context, state) => CustomTransitionPage(
                key: state.pageKey,
                child: const LaunchGatewayScreen(),
                transitionsBuilder: _buildUniversalPageTransition,
              ),
        ),
        GoRoute(
          path: '/main-container',
          pageBuilder:
              (context, state) => CustomTransitionPage(
                key: state.pageKey,
                child: const MainContainerScreen(),
                transitionsBuilder: _buildUniversalPageTransition,
              ),
        ),
        GoRoute(
          path: '/credential-entry',
          pageBuilder:
              (context, state) => CustomTransitionPage(
                key: state.pageKey,
                child: const CredentialEntryScreen(),
                transitionsBuilder: _buildUniversalPageTransition,
              ),
        ),
        GoRoute(
          path: '/account-creation',
          pageBuilder:
              (context, state) => CustomTransitionPage(
                key: state.pageKey,
                child: const AccountCreationScreen(),
                transitionsBuilder: _buildUniversalPageTransition,
              ),
        ),
        GoRoute(
          path: '/kite-culture',
          pageBuilder:
              (context, state) => CustomTransitionPage(
                key: state.pageKey,
                child: const KiteCultureScreen(),
                transitionsBuilder: _buildUniversalPageTransition,
              ),
        ),
        GoRoute(
          path: '/kite-tutorial',
          pageBuilder:
              (context, state) => CustomTransitionPage(
                key: state.pageKey,
                child: const KiteTutorialScreen(),
                transitionsBuilder: _buildUniversalPageTransition,
              ),
        ),
        GoRoute(
          path: '/kite-moment-detail',
          pageBuilder: (context, state) {
            final momentData = state.extra as KiteMomentEntity;
            return CustomTransitionPage(
              key: state.pageKey,
              child: KiteMomentDetailScreen(momentData: momentData),
              transitionsBuilder: _buildUniversalPageTransition,
            );
          },
        ),
        GoRoute(
          path: '/kite-video-player',
          pageBuilder: (context, state) {
            final videoUrl = state.extra as String;
            return CustomTransitionPage(
              key: state.pageKey,
              child: KiteVideoPlayerScreen(videoResourceUrl: videoUrl),
              transitionsBuilder: _buildUniversalPageTransition,
            );
          },
        ),
        GoRoute(
          path: '/kite-topic-detail',
          pageBuilder: (context, state) {
            final topic = state.extra as KiteTopicEntity;
            return CustomTransitionPage(
              key: state.pageKey,
              child: KiteTopicDetailScreen(topic: topic),
              transitionsBuilder: _buildUniversalPageTransition,
            );
          },
        ),
        GoRoute(
          path: '/kite-exchange-detail',
          pageBuilder: (context, state) {
            final exchange = state.extra as KiteExchangeEntity;
            return CustomTransitionPage(
              key: state.pageKey,
              child: KiteExchangeDetailScreen(exchangeData: exchange),
              transitionsBuilder: _buildUniversalPageTransition,
            );
          },
        ),
        GoRoute(
          path: '/kite-team-chat',
          pageBuilder: (context, state) {
            final teamData = state.extra as KiteTeamEntity;
            return CustomTransitionPage(
              key: state.pageKey,
              child: KiteTeamChatScreen(teamData: teamData),
              transitionsBuilder: _buildUniversalPageTransition,
            );
          },
        ),
        GoRoute(
          path: '/kite-private-chat',
          pageBuilder: (context, state) {
            final sessionData = state.extra as KiteChatSessionEntity;
            return CustomTransitionPage(
              key: state.pageKey,
              child: KitePrivateChatScreen(sessionData: sessionData),
              transitionsBuilder: _buildUniversalPageTransition,
            );
          },
        ),
        GoRoute(
          path: '/kite-video-call',
          pageBuilder: (context, state) {
            final sessionData = state.extra as KiteChatSessionEntity;
            return CustomTransitionPage(
              key: state.pageKey,
              child: KiteVideoCallScreen(sessionData: sessionData),
              transitionsBuilder: _buildUniversalPageTransition,
            );
          },
        ),
        GoRoute(
          path: '/release',
          pageBuilder:
              (context, state) => CustomTransitionPage(
                key: state.pageKey,
                child: const ReleaseScreen(),
                transitionsBuilder: _buildUniversalPageTransition,
              ),
        ),
        GoRoute(
          path: '/other-persona',
          pageBuilder: (context, state) {
            final targetUser = state.extra as PersonaEntity;
            return CustomTransitionPage(
              key: state.pageKey,
              child: OtherPersonaScreen(targetUser: targetUser),
              transitionsBuilder: _buildUniversalPageTransition,
            );
          },
        ),
        GoRoute(
          path: '/settings',
          pageBuilder:
              (context, state) => CustomTransitionPage(
                key: state.pageKey,
                child: const SettingsScreen(),
                transitionsBuilder: _buildUniversalPageTransition,
              ),
        ),
      ],
    );
  }

  /// 获取路由器实例
  GoRouter get routerInstance => _routerInstance;

  /// 导航到主容器页面
  static void navigateToMainContainer(BuildContext context) {
    context.go('/main-container');
  }

  /// 导航到启动页面
  static void navigateToLaunchGateway(BuildContext context) {
    context.go('/launch-gateway');
  }

  /// 导航到登录页面
  static void navigateToCredentialEntry(BuildContext context) {
    context.push('/credential-entry');
  }

  /// 导航到注册页面
  static void navigateToAccountCreation(BuildContext context) {
    context.push('/account-creation');
  }

  /// 导航到风筝文化页面
  static void navigateToKiteCulture(BuildContext context) {
    context.push('/kite-culture');
  }

  /// 导航到风筝教程页面
  static void navigateToKiteTutorial(BuildContext context) {
    context.push('/kite-tutorial');
  }

  /// 导航到风筝时刻详情页面
  static void navigateToKiteMomentDetail(
    BuildContext context,
    KiteMomentEntity momentData,
  ) {
    context.push('/kite-moment-detail', extra: momentData);
  }

  /// 导航到风筝视频播放页面
  static void navigateToKiteVideoPlayer(BuildContext context, String videoUrl) {
    context.push('/kite-video-player', extra: videoUrl);
  }

  /// 导航到风筝话题详情页面
  static void navigateToKiteTopicDetail(
    BuildContext context,
    KiteTopicEntity topic,
  ) {
    context.push('/kite-topic-detail', extra: topic);
  }

  /// 导航到风筝交换详情页面
  static void navigateToKiteExchangeDetail(
    BuildContext context,
    KiteExchangeEntity exchange,
  ) {
    context.push('/kite-exchange-detail', extra: exchange);
  }

  /// 导航到风筝团队群聊页面
  static void navigateToKiteTeamChat(
    BuildContext context,
    KiteTeamEntity teamData,
  ) {
    context.push('/kite-team-chat', extra: teamData);
  }

  /// 导航到风筝私人聊天页面
  static void navigateToKitePrivateChat(
    BuildContext context,
    KiteChatSessionEntity sessionData,
  ) {
    context.push('/kite-private-chat', extra: sessionData);
  }

  /// 导航到风筝视频通话页面
  static void navigateToKiteVideoCall(
    BuildContext context,
    KiteChatSessionEntity sessionData,
  ) {
    context.push('/kite-video-call', extra: sessionData);
  }

  /// 导航到发布页面
  static void navigateToReleaseScreen(BuildContext context) {
    context.push('/release');
  }

  /// 导航到他人个人页面
  static void navigateToOtherPersonaScreen(
    BuildContext context,
    PersonaEntity targetPersona,
  ) {
    context.push('/other-persona', extra: targetPersona);
  }

  /// 导航到设置页面
  static void navigateToSettingsScreen(BuildContext context) {
    context.push('/settings');
  }
}
